package com.sinodata.bsm.inmp.task.gab;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.jms.JMSException;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.inmp.bean.gab.DianliangyiBean;
import com.sinodata.bsm.inmp.bean.gab.KongtiaoBean;
import com.sinodata.bsm.inmp.bean.gab.UPSBean;
import com.sinodata.bsm.inmp.bean.gab.WenshiduBean;
import com.sinodata.bsm.inmp.bean.gab.WenshiduParentBean;
import com.sinodata.bsm.inmp.cache.CacheFactory;
import com.sinodata.bsm.inmp.cache.ResCache;
import com.sinodata.bsm.inmp.message.Messenger;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-9-5 上午11:00:52          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@SuppressWarnings("deprecation")
public class WshPropertyCollectJob implements StatefulJob {

    private static final Logger logger = Logger.getLogger(WshPropertyCollectJob.class);

    private final ResCache resCache = (ResCache) CacheFactory.getCache(ResCache.class);

    private static final List<Long> wsdResTypeIdList = Arrays.asList(6021000202000L, 6021000203000L, 6021000204000L, 6021000205000L);

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        //筛选出温湿度的设备资源
        List<Res> wsdResList = resCache.getResByTypeId(wsdResTypeIdList);

        //构建对应res bean
        List<WenshiduParentBean> wsdResBeanList = new ArrayList<WenshiduParentBean>(wsdResList.size());
        for (Res res : wsdResList) {
            wsdResBeanList.add(buildRes(res));
        }

        //获取性能数据
        List<ResPropertyValue> wsdResPropertyValueList = new ArrayList<ResPropertyValue>();
        for (WenshiduParentBean resBean : wsdResBeanList) {
            wsdResPropertyValueList.add(resBean.getPropertyValue());
        }

        try {
            Messenger.getInstance().sendPropertyValue(wsdResPropertyValueList);
        } catch (JMSException e) {
            logger.error("wenshidu>>> send property value occured error", e);
        }
    }

    private WenshiduParentBean buildRes(Res res) {

        Long resTypeId = res.getResTypeId();

        if (resTypeId.equals(6021000202000L)) {//空调
            return new KongtiaoBean(res);
        } else if (resTypeId.equals(6021000203000L)) {//UPS设备
            return new UPSBean(res);
        } else if (resTypeId.equals(6021000204000L)) {//电量仪设备
            return new DianliangyiBean(res);
        } else if (resTypeId.equals(6021000205000L)) {//温湿度
            return new WenshiduBean(res);
        } else {
            return null;
        }
    }
}
